﻿// JavaScript Document
;(function($){
	$.fn.albumSlide = function(options){
		var defaults = {
			pageSize:6,
			optionIndex:-1,
			slideIndex:-1,
			loop:true,
			picIndex:"#picIndex",
			picSum:"#picSum"
		};
		var opts = $.extend(defaults,options);
		
		return this.each(function(){								
			var	contaniner = $(this);
			var	pageSize = opts.pageSize,
				optionIndex = opts.optionIndex,
				slideIndex = opts.slideIndex,
				loop = opts.loop,
				picIndexObj = $(opts.picIndex),
				picSumObj = $(opts.picSum);

			var index_option = optionIndex>-1?optionIndex:0,
				index_view = slideIndex>-1?slideIndex:0,
				index_list = 0,
				imgs_length;

			var	viewArea = contaniner.find(".view"),
				listArea = contaniner.find(".list"),
				textArea = contaniner.find(".text"),
				lpOptions = contaniner.find(".lp-options");

			viewArea.find(".prev").click(function(){
				index_view--;
				index_view=loop?
							index_view<0?imgs_length-1:index_view:
								index_view<0?0:index_view;
				silde(index_view,true);
			}); 
			viewArea.find(".next").click(function(){							  
				index_view++;
				index_view=loop?
							index_view>(imgs_length-1)?0:index_view:
								index_view>(imgs_length-1)?(imgs_length-1):index_view;			
				silde(index_view,true);
			});
			listArea.find(".prev").click(function(){
				index_list-=pageSize ;
				index_list=(index_list<0?0:index_list);
				silde(index_list,false);
			}); 
			listArea.find(".next").click(function(){		 
				index_list+=pageSize;
				index_list=(index_list>(imgs_length-1)?(imgs_length-1):index_list);
				silde(index_list,false);
			});
			listArea.find("li").click(function(){							   
				index = $(this).index();
				index_view = index;
				silde(index,true);
			});
			lpOptions.find("li").click(function(){
				index_view = 0;								
				index_option = $(this).index();
				display_albums();
			});
			
			function display_albums(){
				lpOptions.find("li")
					.eq(index_option).addClass("active")
						   .siblings("li").removeClass("active");
				viewArea.find("ul")
					.eq(index_option).show()
					.siblings("ul").hide();
				silde(index_view,true);
			}
			
			init_albums();
			
			function init_albums(){
				display_albums();
/*				silde(index_view,true);	*/
				listArea.find("ul")
					.css("width",(listArea.find("li").outerWidth(true))*imgs_length);
				listArea.find("li").each(function(index){
					var img_obj = new Image(),
						this_img_obj = listArea.find("li").eq(index).find("img"),
						max_width = listArea.find("li").width(),
						max_height = listArea.find("li").height();
					img_obj.src = this_img_obj.data("src");
					img_obj.onload=function(){
						img_load(img_obj,this_img_obj,max_width,max_height);
					};
				});	
			}
			
			function silde(index,state){
				var width = parseInt(listArea.find(".main-list").width())+
								parseInt(listArea.find("li").css("margin-left"))+
									parseInt(listArea.find("li").css("margin-right"));
				listArea.find("ul")
					.animate({"marginLeft":-width*parseInt(index/pageSize)});
					
				var viewUlView = viewArea.find("ul").eq(index_option);
					imgs_length = viewUlView.find("li").length;
					
				if(state){
					picIndexObj.html(index+1);	
					picSumObj.html(imgs_length);
					viewUlView.find("li")
						.eq(index).fadeIn().addClass("active")
						.siblings("li").hide().removeClass("active");
					listArea.find("li")
						.eq(index).addClass("active")
						.siblings("li").removeClass("active");
					textArea.find("li")
						.eq(index).show()
						.siblings("li").hide();	
					contaniner.find(".slide-desc")
						.eq(index).show()
						.siblings(".slide-desc").hide();

					var img_obj = new Image(),
						this_img_obj = viewUlView.find("li").eq(index).find("img"),
						max_width = viewArea.width();
						max_height = viewArea.height();
					img_obj.src = this_img_obj.data("src");
					if(img_obj.complete) {
						img_load(img_obj,this_img_obj,max_width,max_height);
					}
					img_obj.onload=function(){
						img_load(img_obj,this_img_obj,max_width,max_height);
					};
				}
			}
			
			function img_load(img_obj,this_img_obj,max_width,max_height){
				var width = img_obj.width,
					height = img_obj.height,
					image_rate = width / height,
					m_image_rate = max_width / max_height;
				if(width > max_width || height > max_height){
					if(image_rate > m_image_rate){
						this_img_obj.width(max_width).height(max_width/image_rate);
					}else{			
						this_img_obj.width(max_height * image_rate).height(max_height);
					}
				}else{
					this_img_obj.width(width).height(height);
				}
				this_img_obj.attr("src",img_obj.src);
			}
			
		});
	}
})(jQuery)